package com.ruoyi.develop.project.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;

import javax.annotation.Resource;

import com.ruoyi.develop.goodsCustomer.domain.GoodsCustomer;
import com.ruoyi.develop.goodsCustomer.mapper.GoodsCustomerMapper;
import com.ruoyi.develop.projectCustomer.domain.ProjectCustomer;
import com.ruoyi.develop.projectCustomer.mapper.ProjectCustomerMapper;
import com.ruoyi.develop.projectSalesman.domain.ProjectSalesman;
import com.ruoyi.develop.projectSalesman.mapper.ProjectSalesmanMapper;
import com.ruoyi.system.domain.SysUserPost;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.stereotype.Service;
import com.ruoyi.develop.project.mapper.ProjectMapper;
import com.ruoyi.develop.project.domain.Project;
import com.ruoyi.develop.project.service.IProjectService;

/**
 * 项目信息管理Service业务层处理
 *
 * @author lmm
 * @date 2024-09-28
 */
@Service
public class ProjectServiceImpl implements IProjectService {
    @Resource
    private ProjectMapper projectMapper;
    @Resource
    private SysDeptMapper sysDeptMapper;
    @Resource
    private GoodsCustomerMapper goodsCustomerMapper;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private ProjectSalesmanMapper projectSalesmanMapper;

    @Resource
    private ProjectCustomerMapper projectCustomerMapper;



    /**
     * 查询项目信息管理
     *
     * @param id 项目信息管理主键
     * @return 项目信息管理
     */
    @Override
    public Project selectById(String id) {
        return projectMapper.selectById(id);
    }

    /**
     * 查询项目信息管理列表
     *
     * @param entity 项目信息管理
     * @return 项目信息管理
     */
    @Override
    public List<Project> selectList(Project entity) {
        return projectMapper.selectList(entity);
    }

    /**
     * 新增项目信息管理
     *
     * @param entity 项目信息管理
     * @return 结果
     */
    @Override
    public int insert(Project entity) {
        if (StringUtils.isBlank(entity.getId())) {
            entity.setId(IdUtils.fastSimpleUUID());
        }
        // 新增项目业务员关系表
        insertProjectSalesman(entity);
        // 新增项目与客户关系
        insertProjectCustomer(entity);
        if(StringUtils.isNotBlank(entity.getCode())){
            entity.setCode(entity.getCode().trim());
        }


        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        return projectMapper.insert(entity);
    }

    /**
     * 修改项目信息管理
     *
     * @param entity 项目信息管理
     * @return 结果
     */
    @Override
    public int update(Project entity) {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        String projectId = entity.getId();
        if(StringUtils.isNotBlank(entity.getCode())){
            entity.setCode(entity.getCode().trim());
        }
        // 删除项目关联 用户
        projectSalesmanMapper.deleteSalesmanByProjectId(projectId);
        // 删除项目与客户关系
        projectCustomerMapper.deleteCustomerByProjectId(projectId);
        // 新增项目业务员关系表
        insertProjectSalesman(entity);
        // 新增项目与客户关系
        insertProjectCustomer(entity);
        return projectMapper.update(entity);
    }

    /**
     * 批量删除项目信息管理
     *
     * @param ids 需要删除的项目信息管理主键
     * @return 结果
     */
    @Override
    public int deleteByIds(String[] ids) {
        return projectMapper.deleteByIds(ids);
    }

    /**
     * 删除项目信息管理信息
     *
     * @param id 项目信息管理主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return projectMapper.deleteById(id);
    }

    @Override
    public Project selectProjectByCode(String code) {
        return projectMapper.selectProjectByCode(code);
    }

    @Override
    public boolean checkExistByCode(String code) {
        Project project = projectMapper.selectProjectByCode(code);
        return project == null;
    }


    /**
     * 新增项目与业务员
     *
     * @param project 项目对象
     */
    public void insertProjectSalesman(Project project) {
        String[] salesmanIds = project.getSalesmanIds();
        if (StringUtils.isNotEmpty(salesmanIds)) {
            // 新增项目与业务员
            List<ProjectSalesman> list = new ArrayList<ProjectSalesman>(salesmanIds.length);
            StringBuilder salesmanName = new StringBuilder();
            for (String salesman : salesmanIds) {
                ProjectSalesman ps = new ProjectSalesman();
                ps.setProjectId(project.getId());
                ps.setSalesmanId(salesman);
                SysUser user = sysUserMapper.selectUserById(salesman);
                if(user != null){
                    salesmanName.append(user.getUserName()).append(",");
                }
                list.add(ps);
            }
            project.setSalesmanName(salesmanName.toString());

            projectSalesmanMapper.batchProjectSalesman(list);
        }
    }


    /**
     * 新增项目与客户关系
     *
     * @param project 项目对象
     */
    public void insertProjectCustomer(Project project) {
        String[] customerIds = project.getCustomerIds();
        if (StringUtils.isNotEmpty(customerIds)) {
            // 新增项目与客户
            StringBuilder customerName = new StringBuilder();
            List<ProjectCustomer> list = new ArrayList<ProjectCustomer>(customerIds.length);
            for (String customerId : customerIds) {
                ProjectCustomer pc = new ProjectCustomer();
                pc.setProjectId(project.getId());
                pc.setCustomerId(customerId);
                list.add(pc);
                GoodsCustomer goodsCustomer = goodsCustomerMapper.selectById(customerId);
                if(goodsCustomer != null){
                    customerName.append(goodsCustomer.getName()).append(",");
                }
                project.setCustomName(customerName.toString());

            }
            projectCustomerMapper.batchProjectCustomer(list);
        }
    }


}
